package sg.edu.nus.iss.vmcs.ui.listener;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import sg.edu.nus.iss.vmcs.controller.BrandStoreController;
import sg.edu.nus.iss.vmcs.controller.CoinReceiver;
import sg.edu.nus.iss.vmcs.controller.DispenseController;
import sg.edu.nus.iss.vmcs.controller.TransactionController;
import sg.edu.nus.iss.vmcs.entity.BrandDrink;
import sg.edu.nus.iss.vmcs.entity.DrinksBrand;
import sg.edu.nus.iss.vmcs.ui.CoinInputBox;
import sg.edu.nus.iss.vmcs.ui.CustomerPanel;
import sg.edu.nus.iss.vmcs.ui.DrinkSelectionBox;
import sg.edu.nus.iss.vmcs.util.VMCSException;

public class DrinkSelectionListener implements ActionListener {

	private DrinkSelectionBox drinkSelectionBox;
	private CoinInputBox cbox;
	
	public DrinkSelectionListener(DrinkSelectionBox drinkSelectionBox, CoinInputBox cbox) {
		this.drinkSelectionBox = drinkSelectionBox;
		this.cbox = cbox;
	}

	public void actionPerformed(ActionEvent actionEvent) {
		
		
		
		
		try {
			TransactionController dispCntrl = drinkSelectionBox.getTxnController(); 
			dispCntrl.setDrinkSelectionBox(drinkSelectionBox);
			
			drinkSelectionBox.setDrinkSelected(false);
			drinkSelectionBox.notifyObservers(new Boolean(drinkSelectionBox.isDrinkSelected()));
			
			cbox.setCoinInputEnabled(true);
			cbox.notifyObservers(new Boolean(cbox.isCoinInputEnabled()));
			
			int drinkId = Integer.parseInt(actionEvent.getActionCommand());
			//DrinksBrand brand = CustomerPanel.getBrands().get(drinkId+"");
			//BrandDrink brand = CustomerPanel.getBrands().get(drinkId+"");
			BrandDrink brand = (BrandDrink) BrandStoreController.getInstance().getItem(drinkId).getContent();
			
			drinkSelectionBox.setCurrentlySelectedBrand(brand);
			
			
			dispCntrl.startTransaction(drinkId);
		} catch(VMCSException e) {
			e.printStackTrace();
		}
		
		//DispenseController dc = new DispenseController(drinkSelectionBox);
		//dc.allowSelection(false);
	}

}
